package com.medata.utils;

import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @author ：zb
 * @date ：Created in 2019/11/4 0004 8:50
 * @description：
 */

public class NoteUtils {
    public String url="http://gw.api.taobao.com/router/rest";
    public String appkey="23379939";
    public String secret="b7187205ef05591b9e77ebcb650f0721";
    public String  yzmcode(String code,String telnum) throws ApiException, JSONException {
        TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
        AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
        req.setExtend( "extend" );
        req.setSmsType( "normal" );
        req.setSmsFreeSignName( "芸台购" );
        req.setSmsParamString( "{code:'"+code+"'}" );
        req.setRecNum(telnum);
        req.setSmsTemplateCode( "SMS_176528284" );
        AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
        return "true";

    }

    public static void main(String[] args) throws Exception{
        NoteUtils noteUtils=new NoteUtils();
        String flag = noteUtils.yzmcode("666", "17681616658");
        System.out.println(flag);
    }


    public String returnSuccess(String jsonvalue) throws JSONException {
        JSONObject jsonObject = new JSONObject(jsonvalue);

        Map result = new HashMap();
        Iterator iterator = jsonObject.keys();
        String key = null;
        String value = null;
        while (iterator.hasNext()) {
            key = (String) iterator.next();
            value = jsonObject.getString(key);
            JSONObject jsonObjects = new JSONObject(value);
            Iterator iterators = jsonObjects.keys();
            while (iterators.hasNext()) {
                key = (String) iterators.next();
                value = jsonObjects.getString(key);
                if (key.equals("result")) {
                    JSONObject objects = new JSONObject(value);
                    Iterator its = objects.keys();
                    while (its.hasNext()) {
                        key = (String) its.next();
                        value = objects.getString(key);
                        result.put(key, value);
                    }
                }

            }
        }
        return result.get("success").toString();
    }
}
